package distriKub;

import java.io.*;

public class FileHandler {

	public static final String resultsFileName = "results.bak";

	public static boolean saveResults(String theResults) { // Returns whether the saving action was successful

		boolean savedOK = false;

		try {
			FileWriter outFile = new FileWriter(resultsFileName, false);
			PrintWriter out = new PrintWriter(outFile);
			out.println(theResults);
			out.close();
			savedOK = true;
		} catch (IOException IOExp) {
			savedOK = false;
		} // End of catch block

		return savedOK;
	} // End of saveResults method
	
	public static String loadResults(){ // On error returns null
		
		StringBuilder fileContent = new StringBuilder();
		
		try{
			FileReader input = new FileReader(resultsFileName);
			BufferedReader bufRead = new BufferedReader(input);
			
			String line = null;
			
			do {
				line = bufRead.readLine();
				fileContent.append(line + "\n");
			} while (line != null); 
			
			bufRead.close();
			
		} catch (FileNotFoundException FNFExp) {
			fileContent = null;
		} // End of catch method
		catch (IOException IOExp) {
			fileContent = null;
		}
		
		return fileContent.toString();
	} // End of loadResults method

}